package kuang.pool;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 使用线程池创建
 */
public class Demo01 {
    public static void main(String[] args) {
        //单个线程
        Executors.newSingleThreadExecutor();
        //创建一个固定的线程池大小
        Executors.newFixedThreadPool(5);
        //可伸缩大小
        ExecutorService threadPool = Executors.newCachedThreadPool();

        try {
            for (int i = 0; i < 100; i++) {
                threadPool.execute(() -> {
                    System.out.println(Thread.currentThread().getName());
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 记得关闭线程池
            threadPool.shutdown();
        }
    }
}
